package com.tc.excel;

import com.tc.excel.handler.ExcelArgumentResolvers;
import com.tc.excel.handler.ExcelReturnValueHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import java.util.List;

/**
 * @Auther: tianchao
 * @Date: 2025/2/14 15:50
 * @Description:
 */
// @Configuration
public class ExcelConfiguration2 implements WebMvcConfigurer {

    /**
     * Add resolvers to support custom controller method argument types.
     * <p>This does not override the built-in support for resolving handler
     * method arguments. To customize the built-in support for argument
     * resolution, configure {@link RequestMappingHandlerAdapter} directly.
     *
     * @param resolvers initially an empty list
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(0, excelArgumentResolvers());
    }

    /**
     * Add handlers to support custom controller method return value types.
     * <p>Using this option does not override the built-in support for handling
     * return values. To customize the built-in support for handling return
     * values, configure RequestMappingHandlerAdapter directly.
     *
     * @param handlers initially an empty list
     */
    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        handlers.add(0, excelReturnValueHandler());
    }
    //    @PostConstruct
//    public void setReturnValueHandlers() {
//        List<HandlerMethodReturnValueHandler> returnValueHandlers = requestMappingHandlerAdapter.getReturnValueHandlers();
//        List<HandlerMethodReturnValueHandler> newHandlers = new ArrayList<>();
//        newHandlers.add(excelReturnValueHandler());
//        if (returnValueHandlers != null){
//            newHandlers.addAll(returnValueHandlers);
//        }
//        requestMappingHandlerAdapter.setReturnValueHandlers(newHandlers);
//    }
//    @PostConstruct
//    public void setArgumentResolvers() {
//        List<HandlerMethodArgumentResolver> argumentResolvers = requestMappingHandlerAdapter.getArgumentResolvers();
//        List<HandlerMethodArgumentResolver> newResolvers =  new ArrayList<>();
//        newResolvers.add(excelArgumentResolvers());
//        if (argumentResolvers != null){
//            newResolvers.addAll(argumentResolvers);
//        }
//        requestMappingHandlerAdapter.setArgumentResolvers(newResolvers);
//    }


    @Bean
    public ExcelArgumentResolvers excelArgumentResolvers() {
        return new ExcelArgumentResolvers();
    }
    @Bean
    public ExcelReturnValueHandler excelReturnValueHandler(){
        return new ExcelReturnValueHandler();
    }
}
